package com.tanhua.server.service;

import com.tanhua.commons.templates.HuanXinTemplate;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.vo.ContactVo;
import com.tanhua.domain.vo.MessageVo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.db.UserInfoApi;
import com.tanhua.dubbo.api.mongo.CommentApi;
import com.tanhua.dubbo.api.mongo.FriendApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 消息管理业务逻辑处理层
 */
@Service
public class MessagesService {

    @Reference
    private FriendApi friendApi;

    @Reference
    private UserInfoApi userInfoApi;

    @Reference
    private CommentApi commentApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    /**
     * 联系人添加
     */
    public void saveFriend(long friendId) {
        Long userId = UserHolder.getUserId();
        //a.调用服务往tanhua_users好友表保存记录服务方法
        Friend friend = new Friend();
        friend.setUserId(userId);//当前登录用户id
        friend.setFriendId(friendId);//好友id
        friendApi.saveFriend(friend);
        ///b.调用环信云成为好友userId,friendId对应用户必须在环信云已经注册
        huanXinTemplate.makeFriends(userId,friendId);
    }

    /**
     * 分页查询联系人
     * keyword:搜索联系人列表关键字（前提 app需要发送请求）
     */
    public PageResult<ContactVo> queryFriendPage(int page, int pagesize, String keyword) {
        PageResult<ContactVo> voPageResult = new PageResult<>();

        Long currentUserId = UserHolder.getUserId();//当前登录用户id
        //1.调用分页查询联系人列表功能
        PageResult<Friend> pageResult = friendApi.queryFriendPage(page,pagesize,currentUserId);
        if(pageResult == null || CollectionUtils.isEmpty(pageResult.getItems())){
            return new PageResult<>(0l,10l,0l,1l,null);
        }
        //2.根据联系人id查询userInfo
        List<ContactVo> contactVoList = new ArrayList<>();
        long num = 1l;
        for (Friend friend : pageResult.getItems()) {
            ContactVo contactVo = new ContactVo();
            Long friendId = friend.getFriendId(); //好友id(联系人id)
            UserInfo userInfo = userInfoApi.queryUserInfo(friendId);
            BeanUtils.copyProperties(userInfo,contactVo);// nickname  avatar gender  age city
            contactVo.setId(num);
            contactVo.setUserId(friendId.toString());//好友id
            contactVoList.add(contactVo);
            num++;
        }
        //3.封装Vo返回
        BeanUtils.copyProperties(pageResult,voPageResult);
        voPageResult.setItems(contactVoList);
        return voPageResult;
    }

    /**
     * 点赞列表 1  //评论类型，1-点赞，2-评论，3-喜欢
     */
    public PageResult<MessageVo> messageCommentPage(int page, int pagesize, int commentType) {
        PageResult<MessageVo> voPageResult = new PageResult<>();

        Long publishUserId = UserHolder.getUserId();///当前登录用户id
        //1.调用服务 分页查询评论表 page pagesize  commentType publishUserId
        PageResult<Comment> pageResult = commentApi.messageCommentPage(page,pagesize,commentType,publishUserId);
        if(pageResult == null || CollectionUtils.isEmpty(pageResult.getItems())){
            return new PageResult<>(0l,10l,0l,1l,null);
        }
        //2.根据评论人userId查询userInfo
        List<MessageVo> messageVoList = new ArrayList<>();
        for (Comment comment : pageResult.getItems()) {
            MessageVo messageVo = new MessageVo();
            Long userId = comment.getUserId();//根据此评论人用户id查询userInfo
            UserInfo userInfo = userInfoApi.queryUserInfo(userId);
            messageVo.setId(userId.toString());//编号 评论人用户id
            if(!StringUtils.isEmpty(comment.getCreated())){
                messageVo.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:ss").format(comment.getCreated()));
            }
            BeanUtils.copyProperties(userInfo,messageVo);
            messageVoList.add(messageVo);
        }
        //3.封装Vo返回
        BeanUtils.copyProperties(pageResult,voPageResult);
        voPageResult.setItems(messageVoList);
        return voPageResult;
    }
}
